package com.example.smart_city_0829.activity.waste_sorting;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.TextView;

import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.entity.Base;
import com.example.smart_city_0829.entity.waste_sorting.NewsDetails;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.view.MaxSizeWebView;
import com.google.android.material.textfield.TextInputLayout;

import java.util.HashMap;
import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Response;

public class NewsDetailsActivity extends BaseActivity {

    private int id;
    private NewsDetails.DataBean data;

    private TextView title;
    private TextView sub;
    private TextView tag;
    private MaxSizeWebView web;
    private TextInputLayout til;
    private Button sendBt;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_news_details;
    }

    @Override
    protected void initView() {
        title = (TextView) findViewById(R.id.title);
        sub = (TextView) findViewById(R.id.sub);
        tag = (TextView) findViewById(R.id.tag);
        web = (MaxSizeWebView) findViewById(R.id.web);
        til = (TextInputLayout) findViewById(R.id.til);
        sendBt = (Button) findViewById(R.id.send_bt);

    }

    @Override
    protected void initData() {
        id = getIntent().getIntExtra("id",0);

        api.getGarbageNewsDetail(id).enqueue(new NetCallBack<NewsDetails>() {
            @Override
            public void onResponse(@NonNull Call<NewsDetails> call, @NonNull Response<NewsDetails> response) {
                if (response.body() == null) return;

                data = response.body().getData();

                title.setText(data.getTitle());
                sub.setText(data.getAuthor());
                tag.setText(data.getCreateTime());

                web.loadDataWithBaseURL(null,data.getContent(),null,"utf-8",null);
            }
        });
    }

    @Override
    protected void initEvent() {
        Objects.requireNonNull(til.getEditText()).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //ignore
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (count > 30 ){
                    til.setErrorEnabled(true);
                    til.setError("超出字数限制");
                    sendBt.setEnabled(false);
                }else {
                    til.setErrorEnabled(false);
                    til.setError("");
                    sendBt.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                //ignore
            }
        });
        sendBt.setOnClickListener((v) -> {
            showLoadingDialog(null,null, ProgressDialog.STYLE_SPINNER);
            final HashMap<String, Object> params = new HashMap<>();
            params.put("newsId",id);
            params.put("content", Objects.requireNonNull(til.getEditText()).getText().toString());
            final RequestBody body = RequestBody.create(gson.toJson(params), MediaType.parse("application/json"));
            api.sendGarbageNewsCommend(body).enqueue(new NetCallBack<Base>() {
                @Override
                public void onResponse(Call<Base> call, Response<Base> response) {
                    if (response.body() == null) return;

                    if (response.body().getCode() == 200) {
                        showToastShort("评论成功");
                    }else{
                        showToastShort("评论失败");
                    }
                    progressDialog.dismiss();
                }
            });

            sendBt.setText("");
        });
    }

    @Override
    protected String getActionBarTitle() {
        return null;
    }
}